﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Data;
using System.Web.UI.WebControls;
using DevExpress.Web;
using BLL;

namespace Web.Pages.YongShui._18.weihu
{
    public partial class jh_dingejs_school : System.Web.UI.Page
    {

        #region 变量

        /// <summary>
        /// SQL调用类
        /// </summary>
        BLL.SqlHelper _SqlHelper = new BLL.SqlHelper();

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                LoadData();
            SetReadonly();
        }

        protected void SetReadonly()
        {
            bool bReadonly = !string.IsNullOrEmpty(Request.QueryString["ro"]) && Convert.ToInt32(Request.QueryString["ro"]) == 1;
            btnOp.Visible = !bReadonly;
            txtCBRQ.ReadOnly = txtKR1.ReadOnly = txtKR2.ReadOnly = txtKR3.ReadOnly = txtKR4.ReadOnly = txtKR5.ReadOnly = txtKR6.ReadOnly = bReadonly;
            foreach (RepeaterItem item in rptDEJS.Items)
            {
                ASPxTextBox txtVal1 = item.FindControl("txtVal1") as ASPxTextBox;
                if (null != txtVal1) txtVal1.ReadOnly = bReadonly;
                ASPxTextBox txtVal2 = item.FindControl("txtVal2") as ASPxTextBox;
                if (null != txtVal2) txtVal2.ReadOnly = bReadonly;
                ASPxRadioButtonList rbtSelLs = item.FindControl("rbtSelLs") as ASPxRadioButtonList;
                if (null != rbtSelLs) rbtSelLs.ReadOnly = bReadonly;
            }
        }

        protected void LoadData()
        {
            txtUserID.Text = Request.QueryString["yonghuh"];
            if (string.IsNullOrEmpty(Request.QueryString["ssdx"]))
                hfSSDX.Value = Request.QueryString["ssdx"];
            // 系数和合计信息
            var ds = _SqlHelper.P_YS_DINGEJIS_SYS(txtUserID.Text);
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                DataRow dr = ds.Tables[0].Rows[0];
                txtUserName.Text = dr["S_YONGHUM"].ToString();
                txtCBRQ.Text = dr["i_day"].ToString();
                txtV1.Text = dr["n_v1"].ToString();
                txtV2.Text = dr["n_v2"].ToString();
                txtL1.Text = dr["n_l1"].ToString();
                txtL2.Text = dr["n_l2"].ToString();
                txtKA1.Text = dr["n_ka1"].ToString();
                txtKA2.Text = dr["n_ka2"].ToString();
                txtKR1.Text = dr["n_kr1"].ToString();
                txtKR2.Text = dr["n_kr2"].ToString();
                txtKR3.Text = dr["n_kr3"].ToString();
                txtKR4.Text = dr["n_kr4"].ToString();
                txtKR5.Text = dr["n_kr5"].ToString();
                txtKR6.Text = dr["n_kr6"].ToString();
                txtK1.Text = dr["n_k1"].ToString();
                txtK2.Text = dr["n_k2"].ToString();
                txtK3.Text = dr["n_k3"].ToString();
                txtK4.Text = dr["n_k4"].ToString();
                txtK5.Text = dr["n_k5"].ToString();
                txtK6.Text = dr["n_k6"].ToString();
            }
            // 用户定额计算信息
            ds = _SqlHelper.P_YS_DINGEJIS(txtUserID.Text, hfSSDX.Value);
            rptDEJS.DataSource = ds;
            rptDEJS.DataBind();
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                hfSSDX.Value = ds.Tables[0].Rows[0]["S_SHESHUIDXDM"].ToString();
        }

        protected void rptDEJS_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            DataRowView rowv = (DataRowView)e.Item.DataItem;
            ASPxRadioButtonList rbtSelLs = e.Item.FindControl("rbtSelLs") as ASPxRadioButtonList;
            Repeater rptDEJSMX = e.Item.FindControl("rptDEJSMX") as Repeater;
            if (null != rbtSelLs && null != rptDEJSMX)
            {
                int I_SORT = Convert.ToInt32(rowv["I_SORT"]);

                rbtSelLs.DataSource = rptDEJSMX.DataSource = _SqlHelper.P_YS_DINGEJIS_MX(txtUserID.Text, I_SORT);
                rbtSelLs.DataBind();
                rptDEJSMX.DataBind();

                rbtSelLs.Value = Convert.ToInt32(rowv["I_SEL"]);
            }
            ASPxTextBox txtVal2 = e.Item.FindControl("txtVal2") as ASPxTextBox;
            txtVal2.Visible = !string.IsNullOrEmpty(rowv["s_val2"].ToString());
        }

        protected void btnOp_Click(object sender, EventArgs e)
        {
            // 保存系数信息
            _SqlHelper.Update_YS_YG_DINGEJIS_SYS(txtUserID.Text, Convert.ToInt32(txtCBRQ.Text),
                Convert.ToDecimal(txtK1.Text), Convert.ToDecimal(txtK2.Text), Convert.ToDecimal(txtK3.Text),
                Convert.ToDecimal(txtK4.Text), Convert.ToDecimal(txtK5.Text), Convert.ToDecimal(txtK6.Text),
                Convert.ToDecimal(txtKR1.Text), Convert.ToDecimal(txtKR2.Text), Convert.ToDecimal(txtKR3.Text),
                Convert.ToDecimal(txtKR4.Text), Convert.ToDecimal(txtKR5.Text), Convert.ToDecimal(txtKR6.Text)
                );
            // 循环保存各项
            foreach (RepeaterItem item in rptDEJS.Items)
            {
                HiddenField hf_I_SORT = item.FindControl("hf_I_SORT") as HiddenField;
                ASPxRadioButtonList rbtSelLs = item.FindControl("rbtSelLs") as ASPxRadioButtonList;
                ASPxTextBox txtVal1 = item.FindControl("txtVal1") as ASPxTextBox;
                ASPxTextBox txtVal2 = item.FindControl("txtVal2") as ASPxTextBox;
                if (null != rbtSelLs && null != rbtSelLs.SelectedItem && null != hf_I_SORT && null != txtVal1 && null != txtVal2)
                {
                    _SqlHelper.Update_YS_YG_DINGEJIS_BAS(txtUserID.Text, hfSSDX.Value, Convert.ToInt32(hf_I_SORT.Value),
                        Convert.ToInt32(rbtSelLs.SelectedItem.Value),
                        Convert.ToDecimal(txtVal1.Text), Convert.ToDecimal(txtVal2.Text));
                }
            }
            // 计算
            _SqlHelper.P_YS_DINGEJIS_OP(txtUserID.Text);
            // 刷新数据
            LoadData();
        }
    }
}
